package dashboard;

import java.io.IOException;
import java.io.ObjectStreamException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.*;

@SuppressWarnings("serial")
public class TimerServlet extends HttpServlet {

	private User currentUser;
	private HttpSession session;
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private ActivityController ac;

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		this.req= req;
		this.resp = resp;
		session = req.getSession();	
		currentUser =  (User) session.getAttribute("user");
		User user = UserRegistry.getInstance().getUserById(currentUser.getId());
		
		if(req.getParameter("start")!=null){
			if(user.getTimerStatus()==false){
			startTimer();
			}
			else if (currentUser.getActivities()!=null){
			resp.sendRedirect("dashboard.jsp?t=started");
			}
			else{
			List<String> errors = new ArrayList<String>();
			errors.add("Er liep iets fout.");
			session.setAttribute("errors",errors);
			resp.sendRedirect("timer.jsp");
			}
		}

		else if (req.getParameter("addActivity")!= null){
			stopTimer();			
		}
		else{
			List<String> errors = new ArrayList<String>();
			errors.add("Prepare for selfdestruction :)");
			session.setAttribute("errors", errors);
			resp.sendRedirect("timer.jsp");
		}
		
	}

	public void startTimer() throws IOException, ObjectStreamException{
	
		String type= req.getParameter("type");
		String courseName = req.getParameter("vak");
		ac = new ActivityController();
		List<String> errors= ac.addActivity(courseName, currentUser, type);
	

		if(errors.size()==0){
			Date now = new Date();
			session.removeAttribute("stop");
			session.setAttribute("timerCourse" ,courseName);
			session.setAttribute("time", now);
			session.setAttribute("type", type);
			session.setAttribute("user", UserRegistry.getInstance().getUserById(currentUser.getId()));
			resp.sendRedirect("timer.jsp?t=started");
		}

		else{
			session.setAttribute("errors", errors);
			resp.sendRedirect("timer.jsp");
		}
	}

	public void stopTimer() throws IOException, ObjectStreamException{

		String appreciationString = req.getParameter("appreciation");
		int appreciation = convertIntoInt(appreciationString,10);
		int hours = convertIntoInt(req.getParameter("hours"),24);
		int minutes = convertIntoInt(req.getParameter("minutes"),60);
		int seconds = convertIntoInt(req.getParameter("seconds"),60);
		Date stop = (Date) session.getAttribute("stop");
		String nrOfPages = req.getParameter("nrOfPages");
		String location = req.getParameter("location");
		session.setAttribute("location",location);
		ActivityController ac = new ActivityController();
		
	List<String> errors = ac.endActivity(appreciation, nrOfPages, location, currentUser, hours, minutes, seconds, stop);
		if(errors.size()==0){
			session.setAttribute("user", UserRegistry.getInstance().getUserById(currentUser.getId()));
			session.setAttribute("totalTime", hours+ " hours "+ minutes+ " minutes and "+ seconds +"seconds");
			resp.sendRedirect("timer.jsp?t=stopped");
		}
		else{
			session.setAttribute("errors", errors);
			resp.sendRedirect("timer.jsp?t=stop");
		}
	}

	public int convertIntoInt(String word, int max){
		int i =0;
		while(i<=max){
			if(word.equals(""+i)){
				return i;
			}
			i++;
		}
		return -1;
	}
}